<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<link rel="stylesheet" href="themes/editor/default.css" />
<script charset="utf-8" type="text/javascript" src="js/editor/editor-min.js"></script>
<script charset="utf-8"  type="text/javascript" src="js/editor/editor_zh_CN.js"></script>
<form id="editContractfm" method="post" novalidate>
        <div class="ftitle">修改商业合同</div>
        	<input name="id" type="hidden">
        	<div class="fitem" style="display:none" id="editOpp">
                	<label>商业机会:</label>
                    <select name="addContract_opportunity" id="addContract_opportunity" style="width:150px"  class="easyui-validatebox">
    				</select>
            </div>
            <div class="fitem" id="readOnlyOpp">
                	<label>商业机会:</label>
                    <input id="readOnlyOppName" style="width:145px" readonly>
                    <a href="#" onclick="javascript:editContractJs.editOpp()" class="easyui-linkbutton"  plain="true">修改</a>
            </div>
            <div class="fitem">
            	<label>客户:</label>
                <input name="customer" id="customerName" style="width:145px" readonly>
            </div>
            <div class="fitem">
                <label>价格:</label>
                <input name="price" id="price" class="easyui-numberbox" style="width:145px" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>合同执行时间:</label>
                <input name="time" id="time" type="text" class="easyui-datebox" required="required" style="width:150px">
            </div>
            <div class="fitem">
                <label>当前阶段:</label>
                
               	<select class="easyui-combobox" name="strategy" id="strategy">
                	<option value="processing">进行中</option>
                	<option value="success">完成</option>
                	<option value="failed">取消</option>
                </select>
            </div>
            <div class="fitem">
                <label>电子合同:</label>
				<input class="ke-input-text" type="text" id="contractAttachment" value="" readonly="readonly" style="width:140px"/> <input type="button" id="uploadButton" value="选择文件" />(如不做修改请留空)
				<input name="attachementId" id="attachementId" type="hidden">
            </div>
          	<div class="fitem">
                <label>详细信息:</label>
                <textarea name="note" id="note" style="width:400px;height:200px"></textarea>
            </div>                                                   
        </form>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="editContractJs.saveContract()">确定</a>
            <div id="toolbar" style="padding:5px;height:auto">
    		按员工过滤:
			<input class="easyui-combobox" id="employeeFilterList" name="employeeFilterList" panelHeight="auto" style="width:100px">

        <div id="searchFilter">
        </div>
    </div>
<script type="text/javascript">
(function(editContractJs, $, undefined ) {  
	var start = 0;
	function getID(){
		return <%=request.getParameter("id")%>;
	}
	editContractJs.editOpp = function() {
		$('#editOpp').show();
		$('#readOnlyOpp').hide();
	};
	editContractJs.init = function ()
	{	
		$('#addContract_opportunity').combogrid({
			    panelWidth:650,
			    panelHeight:250,
			    idField:'id',
			    textField:'name',
			    url:null,
			    columns:[[
							{title:'名称',field:'name',width:150},
			             	{title:'客户信息',field:'customerName',width:120,sortable:true},
			             	{title:'当前阶段',field:'strategy',width:80,sortable:true},
			             	{title:'创建时间',field:'createTime',width:150,sortable:true},
			    ]],
			    pagination:true,
			    pageSize:5,
				pageList:[5],
				toolbar:'#toolbar',
				onChange:setCustomerName
		});

		
		var grid = $('#addContract_opportunity').combogrid('grid');
		
		$(grid).datagrid('getPager').pagination({   
		    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
		    onSelectPage : function(pPageIndex, pPageSize) {   
		    	
		        var gridOpts = $(grid).datagrid('options');   
		        gridOpts.pageNumber = pPageIndex;   
		        gridOpts.pageSize = pPageSize;     

		        start = (pPageIndex -1)*pPageSize;
		        postSearch();
		    },
		    onChangePageSize : function(pageSize){
		    	countPerPage = pageSize;
		    }
		});
		workbenchJs.loadData("dispatch/manage/employee/simpleAll",editContractJs.loadEmployeeResult,"查看用户列表");
		workbenchJs.loadData("dispatch/customer/contract/query?id="+getID(),editContractJs.loadContractResult,"查询合同信息");
		workbenchJs.loadData("dispatch/customer/opportunity/all?start=0&size=5",editContractJs.loadCAData,"查询商业机会");
		
		var uploadbutton = KindEditor.uploadbutton({
			button : KindEditor('#uploadButton'),
			 fieldName : 'cfile',
			 url :'file/upload?type=contract',
			 afterUpload : function(data) {
				 //var payload = jQuery.parseJSON(data);
				 if(data.error == '0')
				{
					 $('#contractAttachment').val(data.name);
					 $('#attachementId').val(data.url);
				}else
				{
					$.messager.alert("上传错误",data.message);
				}
				 
			 },

		});
		 uploadbutton.fileBox.change(function(e) {
			 uploadbutton.submit();
		 });
		
	};
	editContractJs.loadContractResult = function(result) {
		
		var payload = jQuery.parseJSON(result.payload.value);
		
        $('#addCustomerfm').form('clear');
        $('#readOnlyOppName').attr('value',payload.opportunityName);
        $('#customerName').attr('value',payload.customerName);
        $("#price").numberbox('setValue',parseInt(payload.price));
        $("#time").datebox('setValue',payload.time);
        $("#strategy").combobox("setValue",payload.strategy);
        $("#note").val(payload.note);
        
	};
	function setCustomerName(newValue, oldValue){
		var grid = $('#addContract_opportunity').combogrid('grid');
		var r = grid.datagrid('getSelected');
		$("#customerName")[0].value=r.customerName;
	}
	editContractJs.loadCAData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	var grid = $('#addContract_opportunity').combogrid('grid');
    		$(grid).datagrid("loaded");
        	$(grid).datagrid('loadData',payload);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	editContractJs.loadEmployeeResult = function(result){
		var payload = jQuery.parseJSON(result.payload.value);
		var employeedata = '';
		employeedata = '[{"label":"*","name":"全部"}';
		$(payload).each(function(index){
			var label = payload[index].name;
			employeedata += ',{"label":"'+label+'","name":"'+label+'"}';
		});
		employeedata += ']';
		employeedata = jQuery.parseJSON(employeedata);
		$('#employeeFilterList').combobox({
			url:null,
			valueField:'label',
			textField:'name',
			multiple:false,
			panelHeight:'auto',
			data:employeedata,
			onSelect:editContractJs.searchByEmployee
		});
	};

	editContractJs.saveContract = function(){
		var contract = {};
		contract.opportunity = parseInt($("#editContractfm input[name='addContract_opportunity']").val());
		contract.time = $("#editContractfm input[name='time']").val();
		contract.price = parseFloat($("#editContractfm input[name='price']").val());
		contract.note = $("#editContractfm textarea[name='note']").val();
		contract.contractAttachment = $("#attachementId").val();
    	contract.strategy = $("#strategy").combobox("getValue");
    	contract.id=getID();
    	
    	var str = $.toJSON(contract);
    	str = 'parameters='+str;
    	
    	if($('#editContractfm').form('validate'))
    	{
    		$.ajax({
                type: "post",
                url: "dispatch/customer/contract/update",
                dataType:"json",
                data: str,
                cache: false,
                success: function (result) {
                	var res = result.result;
                    if(res == "SUCCESS")
                    {
                    	workbenchJs.open1('component/customer/contract.html',this);
                    }else if(res == "COMPONENT_ERROR")
                    {
                    	$.messager.alert("Error",result.payload.value);
                    }else
                    {
                    	$.messager.alert("Error",result.result);
                    }
                    
                }
                
            });
    	}
    };
	var opportunity_employee_search = '';

	editContractJs.searchByEmployee = function(){
		opportunity_employee_search = $("input[name='employeeFilterList']").val();
    	var grid = $('#addContract_opportunity').combogrid('grid');
		$(grid).datagrid("loading");
		//refreshSearchFilter();
		start = 0;
		postSearch();
	};
	function postSearch()
	{
		var str = "";
		str = appendURL(str,"employee="+opportunity_employee_search);
		str = appendURL(str,'start='+start+'&size=5');
		$.ajax({
            type: "post",
            url: "dispatch/customer/opportunity/all",
            dataType:"json",
            data: str,
            cache: false,
            success: function (result) {
            	
            	editContractJs.loadCAData(result);
            }
		});
	}
	function appendURL(url,str1)
	{
		if(typeof(str1) == 'undefined')return url;
		if(url == "" || typeof(url) == 'undefined')url = str1;
		else url+="&"+str1;

		return url;
	}
}( window.editContractJs = window.editContractJs || {}, jQuery ));

$(function(){
	editContractJs.init();
}); 
	</script>
    <style type="text/css">
        #fm,editContractfm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
         .c-label{
            display:inline-block;
            width:150px;
            height:24px;
            padding:0;
            line-height:24px
        }
        
    </style>